package utils

import (
	"crypto/rand"
	"fmt"
)

/*
*
生成随机字符串

	GenerateRandomString 生成一个指定长度的随机字符串

参数:

	length: 随机字符串的长度

返回:

	生成的随机字符串
	错误信息 (如果有)
*/
func GenerateRandomString(length int) (string, error) {
	if length <= 0 {
		return "", fmt.Errorf("length must be greater than 0")
	}

	const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	bytes := make([]byte, length)
	_, err := rand.Read(bytes)
	if err != nil {
		return "", err
	}

	for i, b := range bytes {
		bytes[i] = letters[b%byte(len(letters))]
	}

	return string(bytes), nil
}
